﻿
using Newtonsoft.Json;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Models.Common;
using Models.JsonResultModel;
using ServiceWeb.DBGateway;
using UtilsBase;
using UtilsORM;
using UtilsORM.MySqlData;
using GatewayServer.Common;

namespace GatewayServer.Controllers
{
    public class TaskDefaultRunController : BaseMagerController
    {
        EFYihongIotContext ef = new EFYihongIotContext();
        // GET: Mager_Department
        public IActionResult list(IFormCollection FC, HttpReqModel httpRequest, iot_product reqProduct, int PageIndex = 1, int PageSize = 15, string type = "1")
        {
            List<SugarParameter> sugarParameters = new List<SugarParameter>();
            int totalRecord = 0;
            string sqlwhere = " and d_visible=0 ";


            if (!string.IsNullOrEmpty(reqProduct.p_guid))
            {
                sqlwhere = sqlwhere + "  and dr_p_guid = @dr_p_guid ";
                sugarParameters.Add(new SugarParameter("dr_p_guid", reqProduct.p_guid));
            }
            else
            {
                sqlwhere = sqlwhere + "    and dr_is_template=0 ";
            }


            if (!string.IsNullOrEmpty(httpRequest.name))
            {
                sqlwhere = sqlwhere + " and d_name like CONCAT('%',@name,'%') ";
                sugarParameters.Add(new SugarParameter("name", httpRequest.name));

            }
            if (!string.IsNullOrEmpty(FC["hid_pageIndex"]))
            {
                PageIndex = int.Parse(FC["hid_pageIndex"].ToString());

            }

            if (!string.IsNullOrEmpty(httpRequest.status))
            {
                //设置状态
                sqlwhere = sqlwhere + " and dr_status=@status";
                sugarParameters.Add(new SugarParameter("status", httpRequest.status));
            }


            string sql = "select  a.*,r.*,m.m_name machine_model from iot_device_task_default_run r left join iot_device a on dr_d_guid=d_guid  left join iot_device_model m on m.m_guid=d_model_guid  where 1= 1   " + sqlwhere;
            DataTable dt = MySqlYihongIot.getPageDataTable(PageIndex, PageSize, sql, sugarParameters, "d_id desc", ref totalRecord);


            ViewData["DataList"] = dt;

            ViewBag.PageSize = PageSize;
            ViewBag.RecordCount = totalRecord;//总条数
            ViewBag.PageCount = totalRecord % PageSize == 0 ? totalRecord / PageSize : totalRecord / PageSize + 1;//分的页数
            ViewBag.PageIndex = PageIndex;//当前页码    
            return View(httpRequest);
        }
        public IActionResult edit(HttpReqModel httpReqModel, iot_product reqProduct)
        {
            ViewBag.dr_p_guid = reqProduct.p_guid;
            if (httpReqModel.order == "upd")
            {
                ViewBag.guid = httpReqModel.guid.ToString();
            }
            else
            {
                ViewBag.guid = Guid.NewGuid().ToString();
            }
            ViewBag.order = httpReqModel.order;
            return View();
        }

        public RespResultData saveAll(IFormCollection FC, HttpReqModel httpRequest, iot_device_task_default_run req, iot_product reqProduct)
        {

            new DBIotDeviceTaskDefaultRun().saveAll(FC, httpRequest, LoginUserInfo, req);
            return RespResult.Sucess();
        }

        public RespResultData getDeviceList(HttpReqModel httpRequest, iot_product product)
        {
            if (!string.IsNullOrEmpty(product.p_guid))
            {
                return RespResult.Sucess(new DBIotDevice().getDeviceByProductList(product.p_guid));
            }
            else
            {
                return RespResult.Sucess(new DBIotDevice().getDeviceByNoProductList());
            }
        }
        public RespResultData getCommandList(HttpReqModel httpRequest)
        {
            iot_device device = new DBIotDevice().getDevice(httpRequest.guid);

            return RespResult.Sucess(new DBIotDeviceCommand().getCommandList(device.d_model_guid));
        }
        public RespResultData getModel(HttpReqModel httpRequest)
        {
            return RespResult.Sucess(new DBIotDeviceTaskDefaultRun().getModel(httpRequest.guid));
        }
        public RespResultData changeStatus(HttpReqModel httpReqModel)
        {
            new DBIotDeviceTaskDefaultRun().changeStatus(httpReqModel.guid);
            return RespResult.Sucess();
        }
        public RespResultData del(HttpReqModel httpReqModel)
        {
            new DBIotDeviceTaskDefaultRun().del(httpReqModel.guid);
            return RespResult.Sucess();
        }
    }
}